package com.snap.vseries.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.snap.vseries.R;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;


public class GridItemView extends RelativeLayout {
    private Paint paint;
    private TextView text;
    private CheckBox checkbox;
    private TextView tvStandard;
    private LinearLayout genes;
    private View famDisplay;
    private View vicDisplay;
    private View roxDisplay;
    private View cy5Display;
    private int paintWidth;

    public GridItemView(Context context) {
        super(context);
        addText(context);
        addCheckBox(context);
        addGenes(context);
        addStandard(context);
        setCurrentParams(context);
    }

    public GridItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void setCurrentParams(Context context) {
        setBackgroundColor(ContextCompat.getColor(context, R.color.white));
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
        if (lp == null)
            lp = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
        lp.width = 90;
        lp.height = 50;
        setLayoutParams(lp);
        paintWidth = 1;
        if (paint == null) {
            paint = new Paint();
        }
        paint.setAntiAlias(true);
        paint.setColor(ContextCompat.getColor(context, R.color.gray));
        paint.setStrokeWidth(paintWidth);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.drawLine(0, 0, this.getWidth(), 0, paint);
        canvas.drawLine(paintWidth, this.getHeight(), this.getWidth() - paintWidth, this.getHeight(), paint);
        canvas.drawLine(0, 0, 0, this.getHeight(), paint);
        canvas.drawLine(this.getWidth(), 0, this.getWidth(), this.getHeight(), paint);
    }

    private void addStandard(Context context) {
        if (tvStandard == null) {
            tvStandard = new TextView(context);
        }
        tvStandard.setId(R.id.grid_standard_tv);
        tvStandard.setWidth(20);
        tvStandard.setGravity(Gravity.CENTER);
        tvStandard.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_white_blue1_5));
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(18, 18);
        lp.setMargins(3, 3, 3, 8);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addView(tvStandard, lp);
    }

    private void addGenes(Context context) {
        if (genes == null) {
            genes = new LinearLayout(getContext());
        }
        genes.setId(R.id.genes);
        genes.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, 3);
        params.setMargins(1, 1, 1, 1);
        if (famDisplay == null) {
            famDisplay = new View(context);
        }
        famDisplay.setId(R.id.grid_fam_display_view);
        famDisplay.setBackgroundColor(ContextCompat.getColor(context, R.color.green));
        genes.addView(famDisplay, params);
        if (vicDisplay == null) {
            vicDisplay = new View(context);
        }
        vicDisplay = new View(context);
        vicDisplay.setId(R.id.grid_vic_display_view);
        vicDisplay.setBackgroundColor(ContextCompat.getColor(context, R.color.red));
        genes.addView(vicDisplay, params);
        if (roxDisplay == null) {
            roxDisplay = new View(context);
        }
        roxDisplay = new View(context);
        roxDisplay.setId(R.id.grid_rox_display_view);
        roxDisplay.setBackgroundColor(ContextCompat.getColor(context, R.color.yellow_one));
        genes.addView(roxDisplay, params);
        LinearLayout.LayoutParams paramsBottom = new LinearLayout.LayoutParams(MATCH_PARENT, 3);
        paramsBottom.setMargins(1, 1, 1, 5);
        if (cy5Display == null) {
            cy5Display = new View(context);
        }
        cy5Display = new View(context);
        cy5Display.setId(R.id.grid_cy5_display_view);
        cy5Display.setBackgroundColor(ContextCompat.getColor(context, R.color.gray));
        genes.addView(cy5Display, paramsBottom);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(65, WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        addView(genes, lp);
    }

    private void addCheckBox(Context context) {
        if (checkbox == null) {
            checkbox = new CheckBox(context);
        }
        checkbox.setId(R.id.grid_checkbox_tv);
        checkbox.setGravity(Gravity.CENTER);
        checkbox.setBackground(ContextCompat.getDrawable(context, R.drawable.sel_checkbox_blue));
        checkbox.setButtonDrawable(null);
        checkbox.setClickable(false);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(25, 25);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        addView(checkbox, lp);
    }

    private void addText(Context context) {
        if (text == null) {
            text = new TextView(context);
        }
        text.setId(R.id.grid_tv);
        text.setVisibility(GONE);
        text.setText("全选");
        text.setGravity(Gravity.CENTER);
        text.setTextColor(Color.BLACK);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
        addView(text, lp);
    }

}
